package edu.umd.fosite.demos;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.glu.GLU;

public class LightningDemo extends GLDemo {

	public LightningDemo() {

	}

	@Override
	protected void reshape(GL2 gl, int x, int y, int width, int height) {
		gl.glMatrixMode(GL2.GL_PROJECTION);
		gl.glLoadIdentity();

		GLU glu = new GLU();

		glu.gluPerspective(60, (double) width / height, 0.5, 300);
		glu.gluLookAt(-6, 0, 4, 0, 0, 0, -4, 0, 6);
	}

	private boolean lit = false;

	@Override
	protected void init(GL2 gl) {
		gl.glClearColor(0, 0, 0, 0);
		gl.glShadeModel(GL2.GL_FLAT);
		gl.glEnable(GL2.GL_DEPTH_TEST);

		// gl.glEnable(GL2.GL_MULTISAMPLE);

		gl.glEnable(GL2.GL_BLEND);
		gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA);

		gl.glEnable(GL2.GL_NORMALIZE);

		gl.glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_FILL);

		/* SETTING UP LIGHTS */
		gl.glEnable(GL2.GL_LIGHTING);
		gl.glEnable(GL2.GL_LIGHT0);
		gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE,
				new float[] { 1f, 1f, 1f, 1 }, 0);
		gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_AMBIENT,
				new float[] { 1f, 1f, 1f, 1 }, 0);
		gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, new float[] { -200, 200,
				200 }, 0);

		gl.glEnable(GL2.GL_LIGHT1);
		gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_AMBIENT,
				new float[] { 0.8f, 0.8f, 0.8f, 1 }, 0);
		gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_POSITION, new float[] { 200, 200,
				200 }, 0);
		gl.glDisable(GL2.GL_LIGHT1);

		new Thread(new Runnable() {

			@Override
			public void run() {
				while (true) {
					synchronized (this) {
						try {
							Thread.sleep(500);
							lit = true;
							Thread.sleep(50);
							lit = false;
							Thread.sleep(50);
							lit = true;
							Thread.sleep(100);
							lit = false;
							Thread.sleep(500);
						} catch (Exception e) {

						}
					}
				}
			}
		}).start();

		gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_DIFFUSE, new float[] { .5f, .5f,
				.5f, 1 }, 0);
		gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT, new float[] { .2f, .2f,
				.2f, 1 }, 0);

		gl.glMatrixMode(GL2.GL_PROJECTION);
	}

	@Override
	protected void dispose(GL2 gl) {

	}

	@Override
	protected void display(GL2 gl) {
		gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);

		if (lit)
			gl.glEnable(GL2.GL_LIGHT1);
		else
			gl.glDisable(GL2.GL_LIGHT1);

		gl.glMatrixMode(GL2.GL_PROJECTION);

		gl.glMatrixMode(GL2.GL_MODELVIEW);
		gl.glRotated(0.5, 0, 0, 1);

		gl.glBegin(GL2.GL_QUADS);
		gl.glNormal3d(0, 0, -1);
		gl.glVertex3d(-1, -1, -1);
		gl.glVertex3d(-1, 1, -1);
		gl.glVertex3d(1, 1, -1);
		gl.glVertex3d(1, -1, -1);

		gl.glNormal3d(0, 0, 1);
		gl.glVertex3d(-1, -1, 1);
		gl.glVertex3d(-1, 1, 1);
		gl.glVertex3d(1, 1, 1);
		gl.glVertex3d(1, -1, 1);
		gl.glEnd();

		gl.glBegin(GL2.GL_QUAD_STRIP);
		gl.glVertex3d(-1, -1, -1);
		gl.glVertex3d(-1, -1, 1);
		gl.glNormal3d(-1, 0, 0);
		gl.glVertex3d(-1, 1, -1);
		gl.glVertex3d(-1, 1, 1);
		gl.glNormal3d(0, 1, 0);
		gl.glVertex3d(1, 1, -1);
		gl.glVertex3d(1, 1, 1);
		gl.glNormal3d(1, 0, 0);
		gl.glVertex3d(1, -1, -1);
		gl.glVertex3d(1, -1, 1);
		gl.glNormal3d(0, -1, 0);
		gl.glVertex3d(-1, -1, -1);
		gl.glVertex3d(-1, -1, 1);
		gl.glEnd();
	}

}
